var express = require('express');
var router = express.Router();
const connection = require('../database/mysql')
const path = require('path')
const session = require('express-session')
const jwt = require('jsonwebtoken')
const sql = require('../routehandles/mysqlhandle')
const { request } = require('http');
var restOfTime = 3
const secretKey = "nan"

router.use(session({
    secret:'mima',
    resave: false,
    saveUninitialized: true,
}))

router.post('/login',async function(req,res)
{
    var params = req.body
    let rule = /^1[345789]\d{9}$/
    if(params.username == '' || params.username ==null)
    {
        console.log('www')
        return res.json({msg:'请填写用户名！',result:false})
    }else if (!rule.test(params.username))
    {
        console.log('sss')
        console.log(params.username)
        return res.json({msg:'用户名格式错误',result:false})
    }else if(params.password == '' || params.password == null)
    {
        console.log('eee')
        return res.json({msg:'密码不能为空！',result:false})
    }else{
       var judge1 = await getData(params.username)
       if(judge1)//这里写根据params.username搜出的数据判断是否为空
        {
            console.log('aaaaa')
            return res.json({msg:'该电话号码尚未注册，请先注册！',result:false})
        }else if (await judge(params.username,params.password)){//如果查到的密码正确
            // res.render('yfpage', {})
            console.log("登录成功")
            return res.json({msg:'登录成功！',result:true})
        }else
        {
            console.log('密码错')
            return res.json({msg:'密码错误',result:false})
        }
    }
})
router.post('/loginByCode',async function(req,res)
{
    var params = req.body
    let rule = /^1[345789]\d{9}$/
    if(params.username == '' || params.username ==null)
    {
        console.log('www')
        return res.json({msg:'请填写手机号码！',result:false})
    }else if (!rule.test(params.username))
    {
        console.log('sss')
        console.log(params.username)
        return res.json({msg:'手机号码格式错误',result:false})
    }else if(params.verify == null || params.verify == '')
    {
        console.log(params.username)
        return res.json({msg:'请输入验证码',result:false})
    }else if(params.verify != params.code)//这里是判断验证码是否一致
    {
        console.log(params.code)
        console.log(params.verify)
        return res.json({msg:'验证码不正确',result:false,last:restOfTime})
    }else if(await getData(params.username))//检测以该用户名为key是否为空
    {
        console.log('1111')
        return res.json({msg:'该号码尚未被注册，请前往注册',result:false})
    }else
    {
        console.log('1111')
        // req.session['userInfo'].username = params.username
        return res.json({msg:'登录成功！',result:true})
    }
})

module.exports = router

const judge = async (username,password) => {
    var xx = await sql.sUAPTable(username)
    console.log(xx)
    if (xx[0]['password'] == password)
    {
        return true
    }else 
    {
        return false
    }
}
const getData = async (username) => {
    var xx = await sql.sUAPTable(username)
    console.log(xx)
    var a = (xx.length == 0)
    console.log(a)
    return a
}